<?php
Namespace Cyfy\Modules\Forms;
use Cyfy\Message;

/**
 * Textarea class element.
 *
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class TextArea extends Input
{
    /**
     * Creates a new instance of TextArea.
     *
     * @param string Textarea name
     * @param string Title
     * @param string Default value
     * @param bool Is required
     */
    function __construct($name, $title, $value = '', $required = false)
    {
        $this -> name = $name;
        $this -> title = $title;
        $this -> value = $value;
        $this -> required = $required;
    }

    /**
     * Returns a string containing the textarea.
     * @return string HTML content
     */
    function __toString()
    {
        $this -> loadDescription();
        
        $required = ($this->required) ? " form-validate" : "";
        $star = ($this->required) ? \Cyfy\Modules\Forms\Module :: REQUIRED_CHAR : "";
    
        $output = "<div class=\"form-item form-item-{$this->name}\"> \n";
        $output .= "    <label class=\"form-label-{$this->name}{$required}\" title=\"{$this->description}\" for=\"{$this->name}\">".$this -> title.": {$star}</label> \n";
        $output .= "	<textarea class=\"form-input-{$this->name}\" name=\"{$this->prefix}{$this->name}\">{$this->value}</textarea>";
        $output .= "</div> \n\n";
        return $output;
    }
    
    /**
     * Loads data from post to the object.
     * Fixes some security stuff.
     *
     * @return array array(name => value)
     */
    function loadPostData($prefix)
    {
        // load
        $this -> value = $_POST[$prefix . $this -> name];
        
        // security
        $this -> value = strip_tags($this -> value);
        return array($this -> name => $this -> value);
    }
    
    /** 
     * Validates the data.
     * @return bool
     */
    function validate()
    {
        // required
        if($this->required){
            if($this -> value == "")
            {
                Message :: set(\Cyfy\Modules\Forms\Module :: t(131, array($this -> title)), Message::ERROR);
                return false;
            }
        }
        return true;
    }
}